package br.com.portalecbahia;

import com.google.android.gcm.GCMRegistrar;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;
import br.com.portalecbahia.connectionservices.SynchronizeNotification;
import br.com.portalecbahia.dao.NotificationDAO;
import br.com.portalecbahia.listner.NotificationListeners;
import br.com.portalecbahia.util.PortalECBahiaUtil;

public class ConfigurationActivity extends Activity implements NotificationListeners, OnCheckedChangeListener{

	private ToggleButton tButton;
	private NotificationDAO dao;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.configuration);
		
		dao = new NotificationDAO(this);
		
		
		
		
		tButton = (ToggleButton) findViewById(R.id.toggleButton1);
		setStatusNotification();

		tButton.setOnCheckedChangeListener(this);
		

	}
	
	
	private void setStatusNotification(){
		boolean enable = dao.findStatusNotification();
		if(enable){
			tButton.setChecked(true);
		}else{
			tButton.setChecked(false);
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    	if(PortalECBahiaUtil.haveNetworkConnection(getApplicationContext())){

		    if(isChecked){
		    	Log.i("TESTE", "ON");
		    	dao.update(1);
		    	
		    	final String regId=GCMRegistrar.getRegistrationId(this);

		 	    if (regId.length() == 0) {
		 	      GCMRegistrar.register(this, SplashActivity.SENDER_ID);
		 	    }
		 	    else {
		 	      Log.d(getClass().getSimpleName(), "Existing registration: "+ regId);
		 	      GCMIntentService.id = regId;

				 new SynchronizeNotification(this).execute(true);
		 	    }

		    }else{
			    Log.i("TESTE", "OFF");
		    	dao.update(0);
				new SynchronizeNotification(this).execute(false);


		    }
		    
			setStatusNotification();
    	}else{
			Toast.makeText(getApplicationContext(), getString(R.string.internetOff), Toast.LENGTH_LONG).show();
			setStatusNotification();
    	}

	}
	@Override
	public void onSynchronizeNotificationComplete(Boolean response) {
		// TODO Auto-generated method stub
		
	}

	
}
